const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    pageData: "",
    serviceType: { //售后类型
      type: 0,
      selected: "请选择",
      typeList: [
        {
          value: "退款",
          checked: true
        }, {
          value: "退货",
          checked: false
        }, {
          value: "其他",
          checked: false
        }
      ]
    },
    serviceStatue: { //货物状态
      type: 1,
      selected: "请选择",
      typeList: [
        {
          value: "已收到货",
          checked: true
        }, {
          value: "未收到货",
          checked: false
        }
      ]
    },
    serviceCause: { //申请原因
      type: 2,
      selected: "请选择",
      typeList: [
        {
          value: "缺少配件",
          checked: true
        }, {
          value: "质量问题",
          checked: false
        }, {
          value: "商品与描述不符",
          checked: false
        }, {
          value: "发错货",
          checked: false
        }, {
          value: "不想要了",
          checked: false
        }, {
          value: "其他",
          checked: false
        }
      ]
    },
    clickType: 0, //0 售后类型  1 货物状态  2 申请原因
    typePop: false,
    afterSaleText: false,
    textarea: "", //问题描述
    imgArr: [],
    orderId:'',
    first1: 0,
    first2: 0,
    first3: 0,
  },
  //弹出售后类型选择框
  selectType(e) {
    let that = this;
    that.setData({
      typePop: true,
      clickType: e.target.dataset.type
    })
    if (e.target.dataset.type == 0 && that.data.first1 == 0) {
      that.setData({
        'serviceType.selected': "退款",
        first1:1
      })
    } else if (e.target.dataset.type == 1 && that.data.first2 == 0) {
      that.setData({
        'serviceStatue.selected': "已收到货",
        first2: 1
      })
    } else if (e.target.dataset.type == 2 && that.data.first3 == 0) {
      that.setData({
        'serviceCause.selected': '缺少配件',
        first3: 1
      })
    }
  },
  //选择售后类型
  radioChange(e) {
    let that = this;
    let radioVal = e.detail.value;
    let clickType = that.data.clickType;
    let typeList = [];
    if (clickType == 0) {
      typeList = that.data.serviceType.typeList;
      typeList.forEach(i => {
        if (i.value == radioVal) {
          i.checked = true;
        } else {
          i.checked = false;
        }
      })
      that.setData({
        'serviceType.selected': radioVal || "已收到货",
        'serviceType.typeList': typeList
      })
    } else if (clickType == 1) {
      typeList = that.data.serviceStatue.typeList;
      typeList.forEach(i => {
        if (i.value == radioVal) {
          i.checked = true;
        } else {
          i.checked = false;
        }
      })
      that.setData({
        'serviceStatue.selected': radioVal,
        'serviceStatue.typeList': typeList
      })
    } else if (clickType == 2) {
      typeList = that.data.serviceCause.typeList;
      typeList.forEach(i => {
        if (i.value == radioVal) {
          i.checked = true;
        } else {
          i.checked = false;
        }
      })
      that.setData({
        'serviceCause.selected': radioVal,
        'serviceCause.typeList': typeList
      })
    }
  },
  // 关闭
  close() {
    this.setData({
      typePop: false
    })
  },
  //点击关于售后
  showText() {
    this.setData({
      afterSaleText: true
    })
  },
  //关闭关于售后
  closeText() {
    this.setData({
      afterSaleText: false
    })
  },
  // 问题描述
  textareaIpt(e) {
    if (e.detail.value.length >= 201) {
      app.showToast('字数不能超过200')
    }
    this.setData({
      textarea: e.detail.value.slice(0, 200)
    })
  },
  //添加图片
  addImg() {
    let that = this;
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#FF6867",
      success(res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            that.chooseWxImage('album')
          } else if (res.tapIndex == 1) {
            that.chooseWxImage('camera')
          }
        }
      }
    })
  },
  // 删除图片
  del(e) {
    let that = this;
    let idx = e.target.dataset.idx;
    let imgs = that.data.imgArr;
    wx.showModal({
      title: '提示',
      content: '确认要删除?',
      success(res) {
        if (res.confirm) {
          imgs.splice(idx, 1);
          that.setData({
            imgArr: imgs
          })
        }
      }
    })
  },
  //将图片上传
  chooseWxImage(type) {
    let that = this;
    let imgs = that.data.imgArr;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: [type],
      success(res) {
        let resPath = res.tempFilePaths;
        if (resPath.length > 3) {
          resPath = resPath.splice(0, 3);
          app.showToast('最多支持三张图片')
        } else {
          resPath.forEach(i => {
            wx.uploadFile({
              url: app.buildUrl('/complaint/addimg'),
              header: app.buildHeader(),
              filePath: i,
              name: 'file',
              success(res) {
                let data = JSON.parse(res.data);
                imgs.push(data.data);
                if (imgs.length <= 3) {
                  that.setData({
                    imgArr: imgs
                  })
                } else {
                  app.showToast('最多支持三张图片')
                }
              }
            })
          })
        }
      }
    })
  },
  //提交
  submit() {
    let that = this;
    if (that.data.serviceType.selected == "请选择"){
      app.showToast('请选择售后类型')
    } else if (that.data.serviceStatue.selected == "请选择") {
      app.showToast('请选择货物状态')
    } else if (that.data.serviceCause.selected == "请选择") {
      app.showToast('请选择申请原因')
    } else if (that.data.imgArr.length == 0) {
      app.showToast('请上传图片')
    } else {
      let imgs = []
      that.data.imgArr.forEach(i=>{
        imgs.push(i.url)
      })
      wx.request({
        url: app.buildUrl('/complaint/add'),
        header:app.buildHeader(),
        method:"POST",
        data:{
          order_id: that.data.pageData.id,
          sale_type: that.data.serviceType.selected,
          goods_type: that.data.serviceStatue.selected,
          reason: that.data.serviceCause.selected,
          problem_description: that.data.textarea,
          user_name: that.data.pageData.addressee_name,
          user_phone: that.data.pageData.addressee_phone,
          imgs: imgs.join(",")
        },
        success(res){
          app.showToast(res.data.msg);
          if (res.data.code == 200){
            setTimeout(()=>{
              wx.navigateTo({
                url: '../after_sale/after_sale?id=' + res.data.data
              })
            }, 1000)
          }
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    that.setData({
      orderId:options.id
    })
    wx.request({
      url: app.buildUrl('/order/get-order-info'),
      header: app.buildHeader(),
      data: {
        id: options.id
      },
      success(res) {
        if (res.data.code == 200) {
          that.setData({
            pageData: res.data.data
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  }
})